// pages/promise/promise.js
let app = getApp();
const http = require("../../utils/request");
Page({
  data: {
    enable: false,
    loadingProps: {
      size: '40rpx',
    },
    backTopVisible: false,
    title: "农场信息",
    value: "",
    ids: [],
    isnull: false,
    issearch: false,
    total: 0,
    index: '1',
    ischange: false,
    isdelete: false,
    mode: '',
    dateOneVisible: false,
    amount: 4,
    isopen: true,
    type: ["","?state=TRANSPORTING","?state=HISTORY"],
    isShow: false,
  },
  open() {
    let val = !this.data.isopen;
    this.setData({
      isopen: val,
    })
  },
  getVal(e) {
    let value = e.detail.value
    value = value.replace(/[^\d]/g,'');
    let key = e.target.dataset.key
    this.setData({
      [`${key}`]: value,
      farmValue: [value]
    })
    this.searchTrans();
},
changeVal(e) {
  let value = e.detail.value
  let key = e.target.dataset.key
  value = value.replace(/[^\d]/g,'');
  this.setData({
    [`${key}`]: value,
    farmValue: value
  })
},
  searchTrans() {
    let farm = this.data.farmValue[0] || "";
    let one = this.data.dateOneText.replaceAll('/','-') || "";
    let two = this.data.dateTwoText.replaceAll('/','-') || "";
    let sents = [farm, one, two];
    let sent = {};
    sents.forEach((item,index,arr) => {
      if(item !== ""  && typeof(item) === "string") {
        sent = {
          ...sent,
          [this.cindex(index)]: item,
        }
      }
    })
    let data = [];
    http.getRequest("/admin/dispatch/1/"+this.data.amount+"?",sent,res=>{
      console.log(res);
      for(let i = 0;i<res.data.data.records.length;i++) {
        let d = res.data.data.records[i];
        d.time = d.time.replace("T"," ");
        data.push(d);
      }
      this.setData({
        ids: data,
        issearch: false,
        isnull: false,
        value: "",
        total: res.data.data.records.length,
      })
    },err=>{  })
  },
  showPickerOne(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}OneVisible`]: true,
    });
  },
  hidePickerOne() {
    const { mode } = this.data;
    this.setData({
      [`${mode}OneVisible`]: false,
    });
  },
  onConfirmOne(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    console.log('confirm', value);
    this.setData({
      [mode]: value,
      [`${mode}OneText`]: value,
    });
    this.searchTrans();
    this.hidePickerOne();
  },

  onColumnChangeOne(e) {
    console.log('pick', e.detail.value);
  },
  showPickerTwo(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}TwoVisible`]: true,
    });
  },
  hidePickerTwo() {
    const { mode } = this.data;
    this.setData({
      [`${mode}TwoVisible`]: false,
    });
  },
  onConfirmTwo(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    console.log('confirm', value);
    this.setData({
      [mode]: value,
      [`${mode}TwoText`]: value,
    });
    this.searchTrans();
    this.hidePickerTwo();
  },

  onColumnChangeTwo(e) {
    console.log('pick', e.detail.value);
  },
  onColumnChange(e) {
    console.log('picker pick:', e);
  },
  onIDPicker() {
    this.setData({ farmVisible: true });
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: e.detail.label,
    });
    this.searchTrans();
  },

  onPickerCancel(e) {
    const { key } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  change(e) {
    this.setData({
        ischange: e.detail
      })
    },
    add() {
      app.globalData.operation = 1;
      wx:wx.navigateTo({
        url: './change/change',
        events: false,
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    },
    deleteshow() {
      let is = !this.data.isdelete
      this.setData({
        isdelete: is,
      })
    },
    onLoad() {
      this.setData({
        isShow: false,
      })
    http.getRequest("/admin/dispatch/1/"+this.data.amount,{},res=>{
      this.setData({
        amount: res.data.data.totalRecord
      })
      http.getRequest("/admin/dispatch/1/"+this.data.amount+this.data.type[this.data.index-1],{},res=>{
        console.log(res);
        this.setData({
          ids: res.data.data.records,
          isnull: false,
          value: "",
          isShow: true,
          total: res.data.data.records.length,
        })
      },err=>{  })
      },err=>{  })
  },
  changeindex(e){
    this.setData({
      index: e.detail.value
    })
    this.onLoad()
  },
  onShow() {
    this.onLoad();
  },
  onRefresh() {
    new Promise((res,rej)=>{
      this.setData({ 
        enable: true,
        isShow:false
      });
      res();
    }).then(()=>{
      this.onLoad();
    }).then(()=>{
      this.setData({
        isShow:false
      })
    })
    .then(()=>{
      setTimeout(() => {
        this.setData({ 
          enable: false,
         });
      }, 100);
    })
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },
})